<?php
/**
 * Created by PhpStorm.
 * User: FDG296
 * Date: 2017/8/2
 * Time: 16:42
 */
namespace backend\models;

use Yii;
use yii\db\ActiveRecord;
use common\helps\tools;

class GoodsSpecRelation extends ActiveRecord{
    const STATUS_ENABLE = 1;
    const STATUS_DISABLE = 0;

    public static function tableName(){
        return '{{%goods_spec_relation}}';
    }

    public function rules(){
        return [
            ['use_flg','default','value'=>self::STATUS_DISABLE],
            ['creator','getCreator'],
            ['create_time','date']
        ];
    }

    /**
     * @获取创建人
     */
    public function getCreator(){
        $userInfo = \common\helps\tools::getloginUserInfo();
        if($userInfo){
            $userName = $userInfo->userName;
        }else{
            $userName = '';
        }
        return $userName;
    }

    /**
     * 新增商品规格对应
     * @param $main_goods_id string 主商品id
     * @param $spec_list array() 已选择的规格id数组
     * @param $goods_spec_list array()  规格记录
     * @param $data array()  请求的数据
     */
    public function addGoodsSpecRelation($main_goods_id,$spec_list,$goods_spec_list,$goods_sku_remark,$goods_sku_pic){
        $spec_group_list = GoodsSpecGroup::find()->select('id,spec_type,spec_group_name')->asArray()->all();
        $spec_group = array();
        foreach($spec_group_list as $value){
            $spec_group[$value['id']]['spec_group_name'] = $value['spec_group_name'];
            $spec_group[$value['id']]['spec_type'] = $value['spec_type'];
        }
        $list = array();
        foreach($spec_list as $key=>$value){
            $list[] = [
                $main_goods_id,
                tools::arrayHasValue($goods_sku_remark,$value) ? $goods_sku_remark[$value] : '',
                tools::arrayHasValue($goods_sku_pic,$value) ? $goods_sku_pic[$value] : '',
                $value,
                $goods_spec_list[$value]['spec_name'],
                $goods_spec_list[$value]['spec_val'],
                $goods_spec_list[$value]['spec_group_id'],
                $spec_group[$goods_spec_list[$value]['spec_group_id']]['spec_group_name'],
                $spec_group[$goods_spec_list[$value]['spec_group_id']]['spec_type'],
                $this->getCreator(),
                date('Y-m-d H:i:s',time())
            ];
        }
        $field = ['main_goods_id','remark','color_pic','spec_id','spec_name','spec_val','spec_group_id','spec_group_name','spec_type','creator','create_time'];
        $res = Yii::$app->db->createCommand()->batchInsert('j_goods_spec_relation', $field, $list)->execute();
        if(!$res){
            $result = array();
            return $result;
        }
//         $result_where = ['main_goods_id'=>$main_goods_id,'use_flg'=>1];
//         $result = $this::find()->where($result_where)->asArray()->all();
//         if(empty($result)){
//             $result = array();
//         }
        return true;
    }

    /**
     * 编辑商品规格对应
     * @param $main_goods_id string 主商品id
     * @param $spec_list array() 已选择的规格id数组
     * @param $goods_spec_list array()  规格记录
     * @param $data array()  请求的数据
     */
    public function modGoodsSpecRelation($main_goods_id,$spec_list,$goods_spec_list,$data){
        $spec_group_list = GoodsSpecGroup::find()->select('id,spec_type,spec_group_name')->asArray()->all();
        $spec_group = array();
        foreach($spec_group_list as $value){
            $spec_group[$value['id']]['spec_group_name'] = $value['spec_group_name'];
            $spec_group[$value['id']]['spec_type'] = $value['spec_type'];
        }

        $old_normal_spec_id = explode(',',$data['old_normal_spec_id']);
        $old_color_spec_id = array();
        if(!empty($data['old_color_spec_id'])){//旧的商品规格是否存在颜色规格
            $old_color_spec_id = explode(',',$data['old_color_spec_id']);
        }

        $old_spec_list = $old_normal_spec_id;
        if(!empty($old_color_spec_id)){
            $old_spec_list = array_merge($old_normal_spec_id,$old_color_spec_id);
        }

        $diff_del_spec_list = array_diff($old_spec_list,$spec_list);//减少的spec_id
        $diff_add_spec_list = array_diff($spec_list,$old_spec_list);//新增的spec_id
        if(!empty($diff_del_spec_list)){
            //禁用去掉的spec_id
            $diff_del_where = ['and',['main_goods_id'=>$main_goods_id],['spec_id'=>$diff_del_spec_list]];
            $diff_data['use_flg'] = 0;
            $ret = $this::updateAll($diff_data,$diff_del_where);
            if(!$ret){
                $result = array();
                return $result;
            }
        }

        if(!empty($diff_add_spec_list)){
            //新增spec_id
            $list = array();
            foreach($diff_add_spec_list as $key=>$value){
                $list[] = [$main_goods_id,$value,$goods_spec_list[$value]['spec_name'],$goods_spec_list[$value]['spec_val'],
                    $goods_spec_list[$value]['spec_group_id'],$spec_group[$goods_spec_list[$value]['spec_group_id']]['spec_group_name'],
                    $spec_group[$goods_spec_list[$value]['spec_group_id']]['spec_type'],$this->getCreator(),date('Y-m-d H:i:s',time())];
            }
            $field = ['main_goods_id','spec_id','spec_name','spec_val','spec_group_id','spec_group_name','spec_type','creator','create_time'];
            $res = Yii::$app->db->createCommand()->batchInsert('j_goods_spec_relation', $field, $list)->execute();
            if(!$res){
                $result = array();
                return $result;
            }
        }
        $result_where = ['main_goods_id'=>$main_goods_id,'use_flg'=>1];
        $result = $this::find()->where($result_where)->asArray()->all();

        if(empty($result)){
            $result = array();
        }

        return $result;
    }
    
    //获取商品规格
    public function getSpecList($main_goods_id,$format_id,$color_id){
        $spec_list  = array();
        $spec_format = array();//规格
        $spec_color = array();//颜色
        $where = ['and',['r.use_flg'=>1],['r.main_goods_id'=>$main_goods_id],['or',['r.spec_id'=>$format_id],['r.spec_id'=>$color_id]]];
        //or 的条件用来过滤没有sku的规格
        $result = $this::find()->where($where)->alias('r')->orderBy('r.sort asc')->select('r.spec_id,r.main_goods_id,s.img_url,r.spec_val,r.spec_type')->
        leftJoin('j_goods_spec as s','r.spec_id = s.id')->asArray()->all();
        if(!empty($result)){
            foreach($result as $value){
                /*if($value['spec_type'] == 2){//SIZE
                 $spec_format[] = $value;
                 }
                 if($value['spec_type'] == 1){//COLOR
                 $spec_color[] = $value;
                 }*/
                if(in_array($value['spec_id'],$format_id)){
                    $spec_format[] = $value;
                }
                if(in_array($value['spec_id'],$color_id)){
                    $spec_color[] = $value;
                }
            }
        }
        $spec_list['spec_format'] = empty($spec_format) ? array() : $spec_format;
        $spec_list['spec_color'] = empty($spec_color) ? array() : $spec_color;
        return $spec_list;
    }
}